/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SupplierFrame.java
 *
 * Created on 26 Des 12, 21:46:50
 */
package com.ivanbiz.gui;

import com.ivanbiz.dao.CompanyDAO;
import com.ivanbiz.dao.impl.CompanyDAOImpl;
import com.ivanbiz.helper.GlobalConstant;
import com.ivanbiz.helper.ReadConfig;
import com.ivanbiz.helper.UserSession;
import com.ivanbiz.model.Company;
import com.ivanbiz.model.User;
import java.io.File;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Administrator
 */
public class CompanyFrame extends javax.swing.JInternalFrame {

    CompanyDAO companyDAO = new CompanyDAOImpl();
    ReadConfig readConfig = new ReadConfig();
    String logoName = null;
    String sourceLogo = null;
    String destination = null;
    User user = UserSession.getUser();
    Company company = UserSession.getCompany();

    /** Creates new form SupplierFrame */
    public CompanyFrame() {
        this.setVisible(true);
        initComponents();
        updateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        textName = new javax.swing.JTextField();
        textAlamat = new javax.swing.JTextField();
        textTelp = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        textFax = new javax.swing.JTextField();
        textInisial = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        labelLogo = new javax.swing.JLabel();
        buttonBrowse = new javax.swing.JButton();
        buttonEdit = new javax.swing.JButton();

        setTitle("Company");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Input", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Monospaced", 0, 11))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Monospaced", 1, 14));
        jLabel2.setText("Nama");

        textName.setFont(new java.awt.Font("Monospaced", 1, 14));

        textAlamat.setFont(new java.awt.Font("Monospaced", 1, 14));

        textTelp.setFont(new java.awt.Font("Monospaced", 1, 14));

        jLabel3.setFont(new java.awt.Font("Monospaced", 1, 14));
        jLabel3.setText("Alamat");

        jLabel5.setFont(new java.awt.Font("Monospaced", 1, 14));
        jLabel5.setText("Telp / HP");

        jLabel6.setFont(new java.awt.Font("Monospaced", 1, 14));
        jLabel6.setText("Fax");

        textFax.setFont(new java.awt.Font("Monospaced", 1, 14));

        textInisial.setFont(new java.awt.Font("Monospaced", 1, 14));
        textInisial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textInisialKeyReleased(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Monospaced", 1, 14));
        jLabel7.setText("Cabang");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(textTelp, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                    .addComponent(textAlamat, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                    .addComponent(textInisial, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                    .addComponent(textFax, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                    .addComponent(textName, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(textTelp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(textFax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textInisial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Image", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Monospaced", 0, 11))); // NOI18N

        labelLogo.setFont(new java.awt.Font("Monospaced", 1, 14));

        buttonBrowse.setFont(new java.awt.Font("Monospaced", 1, 14));
        buttonBrowse.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/gui/icon/Browse.jpg"))); // NOI18N
        buttonBrowse.setText("Browse");
        buttonBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBrowseActionPerformed(evt);
            }
        });

        buttonEdit.setFont(new java.awt.Font("Monospaced", 1, 14));
        buttonEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/gui/icon/Edit.jpg"))); // NOI18N
        buttonEdit.setText("Update");
        buttonEdit.setPreferredSize(new java.awt.Dimension(70, 23));
        buttonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(buttonBrowse, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonEdit, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(labelLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonBrowse, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBrowseActionPerformed
        JFileChooser browseImage = null;
        browseImage = new JFileChooser();
        browseImage.setCurrentDirectory(null);
        browseImage.setAcceptAllFileFilterUsed(false);
        browseImage.addChoosableFileFilter(new FileNameExtensionFilter("JPG", "JPG"));
        //browseImage.addChoosableFileFilter(new FileNameExtensionFilter("PNG", "PNG"));
        browseImage.showOpenDialog(browseImage);
        if (browseImage.getSelectedFile() != null) {
            sourceLogo = browseImage.getSelectedFile().getAbsolutePath();
            logoName = browseImage.getSelectedFile().getName();
            labelLogo.setIcon(new ImageIcon(browseImage.getSelectedFile().toString()));
        }
    }//GEN-LAST:event_buttonBrowseActionPerformed

    private void updateData() {
        if (company != null) {
            textName.setText(company.getNama());
            textAlamat.setText(company.getAlamat());
            textTelp.setText(company.getTelp());
            textFax.setText(company.getFax());
            textInisial.setText(company.getAlias());
            labelLogo.setIcon(new ImageIcon(company.getLogo()));
        }
    }

    private void buttonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditActionPerformed
        try {
            Company companys;
            String exLogo = GlobalConstant.EMPTY_STRING;
            destination = System.getProperty(GlobalConstant.USER_DIR) + GlobalConstant.BACK_SPLASH + GlobalConstant.IMAGE + GlobalConstant.BACK_SPLASH + logoName;
            if (UserSession.getCompany() != null) {
                companys = UserSession.getCompany();
                exLogo = companys.getLogo();
                companys.setUpdatedBy(user.getUserName());
                companys.setUpdatedDate(new Date());
                companys.setLogo(logoName == null ? companys.getLogo() : destination);                
            } else {
                companys = new Company();
                companys.setCreatedBy(user.getUserName());
                companys.setCreatedDate(new Date());
                companys.setLogo(logoName == null ? GlobalConstant.EMPTY_STRING : destination);                
            }
            companys.setNama(textName.getText());
            companys.setAlamat(textAlamat.getText());
            companys.setTelp(textTelp.getText());
            companys.setFax(textFax.getText());
            companys.setAlias(textInisial.getText());
            companyDAO.saveOrUpdate(companys);           
            if (logoName != null) {
                readConfig.copyFile(sourceLogo, destination);
                if (exLogo != null || !exLogo.equals(GlobalConstant.EMPTY_STRING)) {
                    if (!exLogo.equals(destination)) {
                        File fileExLogo = new File(exLogo);
                        fileExLogo.delete();
                    }
                }
            }
            JOptionPane.showMessageDialog(null, GlobalConstant.MSG_UPDATE);
            UserSession.setCompany(companys);
        } catch (Exception ex) {
            Logger.getLogger(CompanyFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_buttonEditActionPerformed

    private void textInisialKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textInisialKeyReleased
        if (textInisial.getText().length() >= 3) {
            textInisial.setText(textInisial.getText().substring(0, 3));
        }
    }//GEN-LAST:event_textInisialKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBrowse;
    private javax.swing.JButton buttonEdit;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel labelLogo;
    private javax.swing.JTextField textAlamat;
    private javax.swing.JTextField textFax;
    private javax.swing.JTextField textInisial;
    private javax.swing.JTextField textName;
    private javax.swing.JTextField textTelp;
    // End of variables declaration//GEN-END:variables
}
